package com.lysj.bhp.api.controller;

import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.resource.base.ResourceConst;
import com.lysj.bhp.resource.controller.ResourceController;
import com.lysj.bhp.system.domain.Verify;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.domain.WithDrawAddress;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v2/user")
@Api(value = "ApiSecurityController", tags = "安全管理接口")
public class ApiSecurityController extends ApiContent {


    /**
     * @author drj
     * @date 2018/05/26 11:53
     * @Description:添加安全密码
     */
    @ApiOperation(value = "添加安全密码", notes = "需要登录密码,交易密码，确认交易密码，邮箱验证码")
    @PostMapping("/security_add_password")
    public Resp securityAddPassword(String password, String newPassword, String confirmPassword, String verify) {
        if (ParamUtil.isBlack(password)) {
            return new Resp(Resp.PARAM_ERROR, "请输入登录密码");
        }
        if (!ParamUtil.md5(password).equals(currentUser().getPassword())) {
            return new Resp(Resp.PARAM_ERROR, "登录密码输入错误");
        }
        if (ParamUtil.isBlack(newPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入交易密码");
        }
        if (ParamUtil.isBlack(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请确认交易密码");
        }
        if (!newPassword.equals(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "两次密码输入不一致");
        }
        Verify verifyQuery = verifyRepository.findOne(currentUser().getEmail());
        if (verifyQuery == null) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        if (!verify.equalsIgnoreCase(verifyQuery.getVerify())) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        currentUser().setTradePassword(ParamUtil.md5(newPassword));
        userRepository.save(currentUser());
        return new Resp("添加成功");
    }

    /**
     * @author drj
     * @date 2018/05/26 11:53
     * @Description:修改安全密码
     */
    @ApiOperation(value = "修改安全密码", notes = "需要登录密码,旧交易密码,新交易密码，确认新交易密码,邮箱验证码")
    @PostMapping("/security_edit_password")
    public Resp securityEditPassword(String newPassword, String confirmPassword, String verify) {

        if (ParamUtil.isBlack(newPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入新交易密码");
        }
        if (ParamUtil.isBlack(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请确认新交易密码");
        }
        if (!newPassword.equals(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "两次密码输入不一致");
        }
        Verify verifyQuery = verifyRepository.findOne(currentUser().getEmail());
        if (verifyQuery == null) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        if (!verify.equalsIgnoreCase(verifyQuery.getVerify())) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        if (verifyQuery.getExpires() < System.currentTimeMillis()) {
            return new Resp(Resp.PARAM_ERROR, "验证码已失效");
        }
        currentUser().setTradePassword(ParamUtil.md5(newPassword));
        userRepository.save(currentUser());
        return new Resp("修改成功");
    }

    /**
     * @author drj
     * @date 2018/05/31 17:25
     * @Description
     */
    @ApiOperation(value = "添加默认提币地址", notes = "defaultStatus对应币种的默认地址  1:不是,2:是")
    @PostMapping("/add_default_address")
    public Resp addDefaultAddress(Long withDrawAddressId, int type) {
        if (ParamUtil.isBlack(withDrawAddressId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择提币地址");
        }
        WithDrawAddress withDrawAddress = withDrawAddressRepository.findOne(withDrawAddressId);
        if (null == withDrawAddress) {
            return new Resp(Resp.PARAM_ERROR, "查无此提币地址");
        }
        WithDrawAddress withDrawAddress1 = withDrawAddressRepository.findByUserIdAndDefaultStatusAndType(currentUser().getUserId(), 2, type);
        if (null != withDrawAddress1) {
            withDrawAddress1.setDefaultStatus(1);
            withDrawAddressRepository.save(withDrawAddress1);
        }
        withDrawAddress.setDefaultStatus(2);
        withDrawAddressRepository.save(withDrawAddress);
        return new Resp("添加成功");
    }


    /**
     * @author drj
     * @date 2018/05/31 17:26
     * @Description:取消默认
     */
    @ApiOperation(value = "取消默认提币地址", notes = "defaultStatus对应币种的默认地址  1:不是,2:是")
    @PostMapping("/cancle_default_address")
    public Resp cancleDefaultAddress(Long withDrawAddressId) {
        if (ParamUtil.isBlack(withDrawAddressId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择提币地址");
        }
        WithDrawAddress withDrawAddress = withDrawAddressRepository.findOne(withDrawAddressId);
        if (null == withDrawAddress) {
            return new Resp(Resp.PARAM_ERROR, "查无此提币地址");
        }
        withDrawAddress.setDefaultStatus(1);
        withDrawAddressRepository.save(withDrawAddress);
        return new Resp("取消成功");
    }


    /**
     * @author drj
     * @date 2018/06/02 13:37
     * @Description:上传身份证或者护照证件的图片接口
     */
    @ApiOperation(value = "上传身份证或者护照证件的图片接口", notes = "")
    @PostMapping("/upload_identy_photo")
    public Resp uploadRemarkPhoto(MultipartFile[] file) {
        StringBuilder filenames = new StringBuilder();
        // 可以不上传图片
        for (int i = 0; i < file.length; i++) {
            String filename = System.currentTimeMillis() + "_" + ParamUtil.uuid();
            String path = ResourceConst.BASE_LOCAL_RESOURCE_IMAGES_DIR + "user_identy";
            String realFileName = ResourceController.fileUpload(file[i], path, filename);
            filenames.append(realFileName).append("-");
        }
        Map<String,Object> obj = new HashMap<>();
        obj.put("photo",filenames.toString());
        return new Resp(Resp.SUCCESS, "上传图片成功", obj);
    }


    /**
     * @author drj
     * @date 2018/06/02 13:48
     * @Description:护照证件认证
     */
    @ApiOperation(value = "护照证件认证接口", notes = "")
    @PostMapping("/passport_certify")
    public Resp passportCertify(User model) {

        if (ParamUtil.isBlack(model.getSureName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入姓");
        }
        if (ParamUtil.isBlack(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入名");
        }
        if (ParamUtil.isBlack(model.getPassportNum())) {
            return new Resp(Resp.PARAM_ERROR, "护照证件号码不能为空");
        }
        if (ParamUtil.isBlack(model.getPassportPhoto())) {
            return new Resp(Resp.PARAM_ERROR, "护照证件照片不能为空");
        }
        currentUser().setSureName(model.getSureName());
        currentUser().setName(model.getName());
        currentUser().setPassportNum(model.getPassportNum());
        if (ParamUtil.isBlack(currentUser().getPassportPhoto())) {
            currentUser().setPassportPhoto(model.getPassportPhoto());
        } else {
            currentUser().setPassportPhoto(currentUser().getPassportPhoto() + model.getPassportPhoto());
        }
        userRepository.save(currentUser());
        return new Resp("护照信息认证成功");
    }




    /**
     * @author drj
     * @date 2018/05/26 11:53
     * @Description:修改登录密码
     */
    @ApiOperation(value = "修改登录密码", notes = "需要登录密码,旧交易密码,新交易密码，确认新交易密码,邮箱验证码")
    @PostMapping("/edit_login_password")
    public Resp editLoginPassword(String oldPassword, String newPassword, String confirmPassword, String verify) {

        if (ParamUtil.isBlack(oldPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入旧密码");
        }
        if (!ParamUtil.md5(oldPassword).equals(currentUser().getPassword())) {
            return new Resp(Resp.PARAM_ERROR, "旧密码错误");
        }
        if (ParamUtil.isBlack(newPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入新密码");
        }
        if (ParamUtil.isBlack(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "请输入重复密码");
        }
        if (!newPassword.equals(confirmPassword)) {
            return new Resp(Resp.PARAM_ERROR, "两次密码输入不一致");
        }
        Verify verifyQuery = verifyRepository.findOne(currentUser().getEmail());
        if (verifyQuery == null) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        if (!verify.equalsIgnoreCase(verifyQuery.getVerify())) {
            return new Resp(Resp.PARAM_ERROR, "验证码错误");
        }
        currentUser().setPassword(ParamUtil.md5(newPassword));
        userRepository.save(currentUser());
        return new Resp("修改成功");
    }

}
